package com.school.controller;

import com.school.service.RechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class RechargeController {

    @Autowired
    private RechargeService rechargeService;

    /**
     * 返回充值码页面
     * @return
     */
    @GetMapping("/generateCode")
    public String generateCode() {
        return "generateCode";
    }

    /**
     * 生成充值码
     * @param amount
     * @param model
     * @return
     */
    @PostMapping("/generateCode")
    public String generateCode(@RequestParam int amount, Model model) {
        String rechargeCode = rechargeService.generateRechargeCode(amount);
        model.addAttribute("rechargeCode", rechargeCode);
        return "generateCode";
    }

    /**
     * 返回充值页面
     * @return
     */
    @GetMapping("/recharge")
    public String processRecharge() {
        return "recharge";
    }

    /**
     * 充值
     * @param code
     * @param model
     * @return
     */
    @PostMapping("/recharge")
    public String processRecharge(@RequestParam String code, Model model) {
        String result = rechargeService.processRecharge(code);
        model.addAttribute("result", result);
        return "recharge";
    }

    /**
     * 充值记录
     * @param model
     * @return
     */
    @RequestMapping("/rechargeRecord")
    public String rechargeRecords(Model model) {
        model.addAttribute("records", rechargeService.getRechargeRecords());
        return "rechargeRecord";
    }
}